package cn.iocoder.yudao.module.design.service.designer;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.design.controller.admin.designer.vo.DesignerCreateReqVO;
import cn.iocoder.yudao.module.design.controller.admin.designer.vo.DesignerExportReqVO;
import cn.iocoder.yudao.module.design.controller.admin.designer.vo.DesignerPageReqVO;
import cn.iocoder.yudao.module.design.controller.admin.designer.vo.DesignerUpdateReqVO;
import cn.iocoder.yudao.module.design.convert.designer.DesignerConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.designer.DesignerDO;
import cn.iocoder.yudao.module.design.dal.mysql.designer.DesignerMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.design.enums.ErrorCodeConstants.ER_NOT_EXISTS;

/**
 * 认证设计师 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DesignerServiceImpl implements DesignerService {

    @Resource
    private DesignerMapper erMapper;

    @Override
    public Long createer(DesignerCreateReqVO createReqVO) {
        // 插入
        DesignerDO er = DesignerConvert.INSTANCE.convert(createReqVO);
        erMapper.insert(er);
        // 返回
        return er.getId();
    }

    @Override
    public void updateer(DesignerUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateerExists(updateReqVO.getId());
        // 更新
        DesignerDO updateObj = DesignerConvert.INSTANCE.convert(updateReqVO);
        erMapper.updateById(updateObj);
    }

    @Override
    public void deleteer(Long id) {
        // 校验存在
        this.validateerExists(id);
        // 删除
        erMapper.deleteById(id);
    }

    private void validateerExists(Long id) {
        if (erMapper.selectById(id) == null) {
            throw exception(ER_NOT_EXISTS);
        }
    }

    @Override
    public DesignerDO geter(Long id) {
        return erMapper.selectById(id);
    }

    @Override
    public List<DesignerDO> geterList(Collection<Long> ids) {
        return erMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DesignerDO> geterPage(DesignerPageReqVO pageReqVO) {
        return erMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DesignerDO> geterList(DesignerExportReqVO exportReqVO) {
        return erMapper.selectList(exportReqVO);
    }

}
